package com.cqc.googleplay.view;

import java.util.List;

import com.cqc.googleplay.R;
import com.cqc.googleplay.application.MyApplication;
import com.cqc.googleplay.utils.CommonUtil;

import android.content.Context;
import android.os.SystemClock;
import android.provider.Settings.System;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

public abstract class ContentPager extends FrameLayout {

	public ContentPager(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initContentPager();
	}

	public ContentPager(Context context, AttributeSet attrs) {
		super(context, attrs);
		// this(context, attrs, 0);
		initContentPager();
	}

	public ContentPager(Context context) {
		super(context);
		// this(context,null);
		initContentPager();
	}

	private void initContentPager() {
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		if (loadingView == null) {
			loadingView = View.inflate(MyApplication.getContext(),
					R.layout.page_loading, null);
		}
		addView(loadingView);

		if (emptyView == null) {
			emptyView = View.inflate(MyApplication.getContext(),
					R.layout.page_empty, null);
		}
		addView(emptyView);

		if (errorView == null) {
			errorView = View.inflate(MyApplication.getContext(),
					R.layout.page_error, null);
		}
		addView(errorView);

		if (successView == null) {
			successView = createSuccessView();
		}
		addView(successView);

		// 根据状态显示view
		showPager();

		// 加载数据
		loadDataAndRefresh();
	}

	/**
	 * 请求数据并更新页面
	 */
	private void loadDataAndRefresh() {
		new Thread() {
			@Override
			public void run() {
				super.run();
				// 请求数据
				SystemClock.sleep(1500);
				Object object = requestData();
				mState = checkData(object);
				// 显示数据,主线程运行
				CommonUtil.runOnUIThread(new Runnable() {
					@Override
					public void run() {
						showPager();
					}
				});
			}
		}.start();
	}

	protected PagerState checkData(Object object) {
		if (object == null) {
			mState = PagerState.STATE_ERROR;
		} else {
			if (object instanceof List) {
				if (((List) object).size() == 0) {
					mState = PagerState.STATE_EMPTY;
				} else {
					mState = PagerState.STATE_SUCCESS;
				}
				mState = PagerState.STATE_SUCCESS;
			} else {
				mState = PagerState.STATE_SUCCESS;
			}
		}
		return mState;
	}


	private void showPager() {
		hideAllView();
		switch (mState.getValue()) {
		case 1:
			loadingView.setVisibility(View.VISIBLE);
			break;
		case 2:
			successView.setVisibility(View.VISIBLE);
			break;
		case 3:
			emptyView.setVisibility(View.VISIBLE);
			break;
		case 4:
			errorView.setVisibility(View.VISIBLE);
			break;
		}
	}

	private void hideAllView() {
		if (loadingView != null) {
			loadingView.setVisibility(View.GONE);
		}
		if (successView != null) {
			successView.setVisibility(View.GONE);
		}
		if (emptyView != null) {
			emptyView.setVisibility(View.GONE);
		}
		if (errorView != null) {
			errorView.setVisibility(View.GONE);
		}
	}

	/**
	 * 请求数据
	 * @return
	 */
	public abstract Object requestData();
	/**
	 * 每个fragment的view不同
	 * @return
	 */
	public abstract View createSuccessView();

	/**
	 * 定义4中加载状态：加载中 + 加载成功 + 加载失败 + 数据为null
	 * 
	 * @author cui
	 */
	public enum PagerState {
		STATE_lOADIGN(1), STATE_SUCCESS(2), STATE_EMPTY(3), STATE_ERROR(4);

		private int value;

		PagerState(int value) {
			this.value = value;
		}

		public int getValue() {
			return value;
		}
	}

	/**
	 * 4中状态对应的view
	 */
	private View loadingView;
	private View successView;
	private View emptyView;
	private View errorView;

	private PagerState mState = PagerState.STATE_lOADIGN;

}
